﻿namespace Mp3Observer.Common.Behaviors
{
    #region usings

    using System.Windows.Automation.Peers;
    using System.Windows.Automation.Provider;
    using System.Windows.Controls.Primitives;
    using System.Windows.Input;
    using System.Windows.Interactivity;

    #endregion

    /// <summary>
    /// simulate button click on targeted button control, when user press "Enter" key in a TextBox control.
    /// </summary>
    /// <example>
    /// <code>
    /// <![CDATA[ 
    ///         <Button x:Name="TargetedButton" Content="Targeted Button"  />
    ///         <TextBox Text="TextBox" >
    ///            <i:Interaction.Triggers>
    ///              <i:EventTrigger EventName="KeyDown" >
    ///                 <behavior:TextBoxEnterButtonInvoke TargetName="TargetedButton" />
    ///              </i:EventTrigger>
    ///            </i:Interaction.Triggers>
    ///        </TextBox>
    /// ]]>
    /// </code>
    /// </example>
    public class TextBoxEnterButtonInvoke : TargetedTriggerAction<ButtonBase>
    {
        /// <summary>
        /// Gets or sets the peer.
        /// </summary>
        /// <value>The AutomationPeer.</value>
        private AutomationPeer Peer { get; set; }

        /// <summary>
        /// Gets or sets the target button
        /// </summary>
        private ButtonBase TargetedButton { get; set; }

        /// <summary>
        /// Called after the TargetedTriggerAction is attached to an AssociatedObject.
        /// </summary>
        /// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
        protected override void OnAttached()
        {
            base.OnAttached();
            this.TargetedButton = this.Target;
            if (this.TargetedButton == null)
                return;

            // set peer
            this.Peer = FrameworkElementAutomationPeer.FromElement(this.TargetedButton);
            if (this.Peer == null)
                this.Peer = FrameworkElementAutomationPeer.CreatePeerForElement(this.TargetedButton);
        }

        /// <summary>
        /// Called after targeted Button change.
        /// </summary>
        /// <param name="oldTarget">The old target.</param>
        /// <param name="newTarget">The new target.</param>
        /// <remarks>Override this to hook up functionality to the new targeted Button.</remarks>
        protected override void OnTargetChanged(ButtonBase oldTarget, ButtonBase newTarget)
        {
            base.OnTargetChanged(oldTarget, newTarget);
            this.TargetedButton = newTarget;
            if (this.TargetedButton == null)
                return;

            // set peer
            this.Peer = FrameworkElementAutomationPeer.FromElement(this.TargetedButton);
            if (this.Peer == null)
                this.Peer = FrameworkElementAutomationPeer.CreatePeerForElement(this.TargetedButton);
        }

        /// <summary>
        /// Invokes the targeted Button when Enter key is pressed inside TextBox.
        /// </summary>
        /// <param name="parameter">KeyEventArgs with Enter key</param>
        protected override void Invoke(object parameter)
        {
            var keyEventArgs = parameter as KeyEventArgs;
            if (keyEventArgs != null && keyEventArgs.Key == Key.Enter)
            {
                if (this.Peer != null && this.TargetedButton != null && this.TargetedButton.IsEnabled)
                {
                    IInvokeProvider invokeProvider = this.Peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
                    invokeProvider.Invoke();
                }
            }
        }
    }
}
